package com.air.designmodel.three.jdk;

import com.air.designmodel.three.DisplayElement;

import java.util.Observable;
import java.util.Observer;

/**
 * 使用jdk内置观察者模式
 *
 * @author shent
 */
public class JdkCurrentConditionsDisplay implements Observer, DisplayElement {

    Observable observable;
    private float temperature;
    private float humidity;

    public JdkCurrentConditionsDisplay(Observable observable) {
        this.observable = observable;
        observable.addObserver(this);
    }

    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof JdkWeatherData) {
            JdkWeatherData jdkWeatherData = (JdkWeatherData) o;
            this.temperature = jdkWeatherData.getTemperature();
            this.humidity = jdkWeatherData.getHumidity();
            display();
        }
    }

    @Override
    public void display() {
        System.out.println(temperature + "华氏度," + humidity + "%湿度");
    }
}
